//
//  TextFieldView.m
//  OCDemo
//
//  Created by zhumuzhi on 2021/7/19.
//

#import "TextFieldView.h"

static CGFloat MARGIN = 20.0f;

@interface TextFieldView ()<UITextFieldDelegate>

@property (nonatomic, assign) CGFloat       margin;
@property (nonatomic, strong) UIButton      *rightView;
@property (nonatomic, strong) UITextField   *textField;

@end

@implementation TextFieldView

#pragma mark - SetData
- (void)setInputType:(TextFieldInputType)inputType {
    _inputType = inputType;
    _textField.utils_inputType = inputType;
}

- (void)setLimitLegnth:(NSInteger)limitLegnth {
    _limitLegnth = limitLegnth;
    _textField.utils_limitLegnth = limitLegnth;
}

- (void)setCornerRadius:(CGFloat)cornerRadius {
    _cornerRadius = cornerRadius;
    _margin = cornerRadius;
    self.layer.cornerRadius = cornerRadius;
    self.layer.masksToBounds = YES;
}

-(void)setRightViewMode:(RightViewShowType)rightViewMode {
    _rightViewMode = rightViewMode;
    
    if (rightViewMode == RightViewShowTypeNever) {
        _rightView.hidden = YES;
    }else if (rightViewMode == RightViewShowTypeEditing) {
        _rightView.hidden = NO;
    }else if (rightViewMode == RightViewShowTypeAlways) {
        _rightView.hidden = NO;
    }
}

- (void)setNormalImage:(UIImage *)normalImage {
    _normalImage = normalImage;
    [_rightView setImage:self.normalImage forState:UIControlStateNormal];
}

- (void)setHighlightedImage:(UIImage *)highlightedImage {
    _highlightedImage = highlightedImage;
    [_rightView setImage:self.highlightedImage forState:UIControlStateHighlighted];
}


#pragma mark - Event
- (void)clickRightView {
    NSLog(@"点击了右侧View");
}

#pragma mark - UITextFieldDelegate

// 输入框-控是否能被编辑，NO不可编辑，YES可编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    // 不可编辑时输入框不会获取光标，可以提前设置一些输入框禁用条件
    NSLog(@"输入框-控是否能被编辑，NO不可编辑，YES可编辑 == %s", __func__);
    
    if (self.shouldBeginEditingBlock) {
        self.shouldBeginEditingBlock(textField);
    }
    
    return YES;
}

// 输入框-开始编辑-点击输入框触发（获取焦点，键盘弹出）
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    NSLog(@"输入框-开始编辑-点击输入框（获取焦点）触发 == %s", __func__);
    
    if (self.didBeginEditingBlock) {
        self.didBeginEditingBlock(textField);
    }
    
}

// 输入框-文字发生变化时触发（文字改变-触发）
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSLog(@"%s == 输入框-文字发生变化时触发（文字改变-触发）", __func__);
    
//    if(self.textFieldShouldChangeBlock) {
//        self.textFieldShouldChangeBlock(textField, range, string);
//    }
    
    return YES;
}

// 输入框-是否允许结束编辑，允许失去第一响应者（键盘是否可以回收）
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    NSLog(@"%s == 输入框-是否允许结束编辑，允许失去第一响应者（键盘是否可以回收）", __func__);
    return YES;
}

// 输入框-结束编辑，失去第一响应者时触发（键盘收回之后）
- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSLog(@"%s == 输入框-结束编辑时触发（键盘收回之后触发）", __func__);
    
    if(self.didEndEditingBlock) {
        self.didEndEditingBlock(textField);
    }
    
}

// 点击键盘 换行（确定/搜索）键
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    NSLog(@"点击键盘【换行（确定/搜索）键】== %s", __func__);
    [textField resignFirstResponder];
    // 一般在此方法中回收键盘 [textField resignFirstResponder]; 也可做事件拦截
    
    if (self.shouldReturnBlock) {
        self.shouldReturnBlock(textField);
    }
    
    return YES;
}

// 点击输入框内【清除按钮】，YES删除输入框内容，NO无操作
- (BOOL)textFieldShouldClear:(UITextField *)textField {
    NSLog(@"点击输入框内【清除按钮】，YES删除输入框内容，NO无操作 == %s", __func__);
    // 可以在点击按钮删除时做事件拦截做一些操作
    return YES;
}

#pragma mark - Init
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = UIColorFromRGB(0xF6F6F6);
        
        [self addSubview:self.rightView];
        [self addSubview:self.textField];
        
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(textFieldTextDidBeginEditingNotification:)
                                                     name:UITextFieldTextDidBeginEditingNotification
                                                   object:self.textField];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(textFieldTextDidChangeNotification:)
                                                     name:UITextFieldTextDidChangeNotification
                                                   object:self.textField];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(textFieldTextDidEndEditingNotification:)
                                                     name:UITextFieldTextDidEndEditingNotification
                                                   object:self.textField];
        
    }
    return self;
}


#pragma mark - kNotificationMethod
- (void)textFieldTextDidBeginEditingNotification:(NSNotification *)notification {
    // textFieldDidBeginEditing
    NSLog(@"输入框-获取焦点 kNotificationMethod - %s", __func__);
}

- (void)textFieldTextDidChangeNotification:(NSNotification *)notification {
    // shouldChangeCharactersInRange
    NSLog(@"输入框-内容变化 kNotificationMethod - %s", __func__);
}

- (void)textFieldTextDidEndEditingNotification:(NSNotification *)notification {
    // textFieldDidEndEditing
    NSLog(@"输入框-文字输入完毕 kNotificationMethod - %s", __func__);
}


- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.right.equalTo(self).offset(-self.margin);
        make.width.mas_equalTo(20);
    }];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(self.margin);
        make.top.bottom.equalTo(self);
        make.right.equalTo(self.rightView.mas_left);
    }];
    
}


#pragma mark - LazyGet
- (UIButton *)rightView {
    if (!_rightView) {
        _rightView = [[UIButton alloc] init];
        [_rightView addTarget:self action:@selector(clickRightView) forControlEvents:UIControlEventTouchUpInside];
        _rightView.imageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _rightView;
}

- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        _textField.delegate = self;
        _textField.utils_limitLegnth = 11;
        _textField.utils_inputType = TextFieldInputTypeDefault;
        _textField.backgroundColor = UIColorFromRGB(0xF6F6F6);
        _textField.clearButtonMode = UITextFieldViewModeAlways;
        _textField.returnKeyType = UIReturnKeySearch;
    }
    return _textField;
}

- (CGFloat)margin {
    if (!_margin) {
        _margin = MARGIN;
    }
    return _margin;
}


@end
